package com.example.springbook.Service;

import com.example.springbook.Dao.BookDao;
import com.example.springbook.Model.BookInfo;
import com.example.springbook.Model.PageRequest;
import com.example.springbook.Model.PageResult;
import com.example.springbook.enums.BookStatus;
import com.example.springbook.mapper.BookInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Component //交给spring进行管理
public class BookService {
    @Autowired
    private BookDao bookDao;
    @Autowired
    private BookInfoMapper bookInfoMapper;
    public List<BookInfo> getBookList(){

        List<BookInfo> books=bookDao.mockData();
        for (BookInfo book : books) {
            if (book.getStatus() == 1) {
                book.setStatusCN("可借阅");
            } else {
                book.setStatusCN("不可借阅");
            }
        }
        return books;
    }

    public Integer insertBook(BookInfo bookInfo){
       return bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        List<BookInfo> bookInfo = bookInfoMapper.getBookListByPage(pageRequest.getOffest(), pageRequest.pageSize);
        Integer count=bookInfoMapper.getBookCount();

        for (BookInfo book : bookInfo) {
           book.setStatusCN(BookStatus.getStatus(book.getStatus()).getDesc());
        }

        return new PageResult<>(bookInfo,count,pageRequest);
    }
}
